package uni.UNIBEE7B8D.Presenter;

import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONException;

import java.text.ParseException;

import uni.UNIBEE7B8D.CommonTool.HttpUrl;
import uni.UNIBEE7B8D.CommonTool.OkHttpUtil;
import uni.UNIBEE7B8D.Enty.ClassCon;
import uni.UNIBEE7B8D.Enty.KechengCon;
import uni.UNIBEE7B8D.Enty.ZhiboContent;
import uni.UNIBEE7B8D.view.Study.ZhiboContent.ZhiboContentContract;

public class ZhiboConPersenter implements ZhiboContentContract.ZhiboConPersenter {
    ZhiboContentContract.ZhiboConView zhiboConView;

    public ZhiboConPersenter(ZhiboContentContract.ZhiboConView zhiboConView) {
        this.zhiboConView = zhiboConView;
    }

    @Override
    public void GetData(String id) {
        Log.d("==",HttpUrl.zhiboCon + "?id=" + id);
        new OkHttpUtil().getJson(HttpUrl.zhiboCon + "?id=" + id, new OkHttpUtil.HttpCallBack() {
            @Override
            public void onSusscess(String data) throws JSONException, ParseException {
                Log.d("==",data);
                ZhiboContent zhiboContent = new Gson().fromJson(data,ZhiboContent.class);
                if(zhiboContent.isSuccess()){
                    zhiboConView.GetData(zhiboContent);
                }else{
                    zhiboConView.Toast(zhiboContent.getMessage());
                }
            }
        });

    }

    @Override
    public void GetClass(String id) {
        new OkHttpUtil().getJson(HttpUrl.banjiCon + "?id=" + id, new OkHttpUtil.HttpCallBack() {
            @Override
            public void onSusscess(String data) throws JSONException, ParseException {
                ClassCon classCon = new Gson().fromJson(data,ClassCon.class);
                if(classCon.isSuccess()){
                    zhiboConView.GetClass(classCon);
                }else{
                    zhiboConView.Toast(classCon.getMessage());
                }

            }
        });
    }

    @Override
    public void GetKecheng(String id) {
        new OkHttpUtil().getJson(HttpUrl.kechengCon + "?id=" + id, new OkHttpUtil.HttpCallBack() {
            @Override
            public void onSusscess(String data) throws JSONException, ParseException {
                KechengCon kechengCon = new Gson().fromJson(data,KechengCon.class);
                if(kechengCon.isSuccess()){
                    zhiboConView.GetKecheng(kechengCon);
                }else{
                    zhiboConView.Toast(kechengCon.getMessage());
                }
            }
        });
    }
}
